<template>
  <div class='tags-container'>
    <el-card>
      <el-row :gutter="15">
        <el-form :model="form" size="medium" label-width="100px" ref="formRef">
          <el-col :span="6">
            <el-form-item label="标签名称" prop="tagName">
              <el-input v-model="form.tagName" clearable :style="{ width: '100%' }"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态" prop="state">
              <el-select v-model="form.state" clearable :style="{ width: '100%' }">
                <el-option v-for="(item, index) in stateOptions" :key="index" :label="item.label" :value="item.value"
                  :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <!-- <el-form-item size="medium"> -->
            <el-button @click="resetForm">清除</el-button>
            <el-button type="primary" @click="searchFn">搜索</el-button>
            <!-- </el-form-item> -->
          </el-col>
        </el-form>
        <el-button type="primary" class="addBtn" style="margin-right:0px" size="medium" @click="addFn">新增标签</el-button>
      </el-row>
      <el-alert type="info" show-icon>
        <slot>
          数据一共{{ totals }}条
        </slot>
      </el-alert>
      <!-- 表格渲染内容 -->
      <el-table :data="tableData" style="width: 100%; margin-top:15px"
        :header-cell-style="{ backgroundColor: '#efefef' }">
        <el-table-column label="序号" type="index">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科">
        </el-table-column>
        <el-table-column prop="tagName" label="标签名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template v-slot="{ row }">
            {{ row.addDate | formatDate }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template v-slot="{ row }">
            {{ row.state === 0 ? '已禁用' : '已启用' }}
          </template>
        </el-table-column>
        <el-table-column prop label="操作">
          <template v-slot="{ row }">
            <el-button type="text" @click="switchState(row.id, row.state)">
              {{ row.state === 0 ? '启用' : '禁用' }}
            </el-button>
            <el-button type="text" :disabled="row.state === 0 ? false : true" @click="changeFn(row)">修改</el-button>
            <el-button type="text" :disabled="row.state === 0 ? false : true" @click="deleteFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end">
        <el-pagination background layout="total, sizes, prev, pager, next, jumper" :total="totals"
          :page-sizes="[10, 20, 30, 40]" :page-size="10" @current-change="changePage" @size-change="changeSize">
        </el-pagination>
      </el-row>
    </el-card>
    <TagsEdit :showDialog.sync="showDialog" ref="tagsEditRef" :currentId="currentId" @success="getTagsLists()">
    </TagsEdit>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/tags'
// import { detail } from '@/api/hmmm/directorys'
import TagsEdit from '../components/tags-edit.vue'
export default {
  name: 'tagsPage',
  components: { TagsEdit },
  data () {
    return {
      form: {
        tagName: undefined,
        state: undefined
      },
      stateOptions: [{
        label: '启用',
        value: 1
      }, {
        label: '禁用',
        value: 0
      }],
      tableData: [],
      totals: undefined,
      currentId: undefined,
      state: false,
      showDialog: false,
      page: {
        page: 1,
        pagesize: 10
      }
      // colloterState: false
    }
  },
  created () {
    this.getTagsLists()
  },
  methods: {
    // 请求标签列表数据
    async getTagsLists () {
      const res = await list(this.page)
      // console.log(res)
      this.tableData = res.data.items
      this.totals = res.data.counts
    },
    // 搜索功能逻辑
    async searchFn () {
      const res = await list({
        tagName: this.form.tagName,
        state: this.form.state,
        page: this.page.page,
        pagesize: this.page.pagesize
      })
      console.log(res)
      this.tableData = res.data.items
      this.totals = res.data.counts
      // console.log(this.$refs)
      // this.$refs.formRef.form(async valid => {
      //   if (!valid) return
      //   // 请求数据
      //   await list({
      //     tagName: this.form.tagName,
      //     state: this.form.state
      //   })
      //   // 刷新列表
      //   this.getTagsLists()
      //   // TODO 提交表单
      // })
      // this.$refs.formRef.form.validate(async valid => {
      //   // this.currentState =
      //   if (!valid) return
      //   // 请求数据
      //   await list({
      //     tagName: this.form.tagName,
      //     state: this.form.state
      //   })
      //   // 刷新列表
      //   this.getTagsLists()
      //   // TODO 提交表单
      // })
    },
    // 清除功能逻辑
    resetForm () {
      this.$refs.formRef.resetFields()
    },
    // 切换标签操作状态功能
    async switchState (id, state) {
      // console.log('state', state)
      this.currentId = id
      this.currentState = state === 0 ? 1 : 0
      await changeState({
        id: this.currentId,
        state: this.currentState
      })
      this.$message.success('操作成功')
      // console.log(res1)
      this.getTagsLists()
    },
    // 标签修改功能实现
    async changeFn (row) {
      // console.log('id', id)
      // console.log('row', row)
      // // 数据回显
      this.currentId = row.id
      // await detail(row.id)
      this.$refs.tagsEditRef.form = row
      // this.$refs.tagsEditRef.subjectNameOptions = this.tableData
      // console.log('tableData', tableData)
      this.showDialog = true
    },
    // 删除功能的实现
    deleteFn (id) {
      console.log('id', id)
      // this.currentId = row.id
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        await remove({ id: id })
        this.$message.success('删除成功')
        // 刷新列表
        this.getTagsLists()
      })
    },
    // 新增标签功能逻辑
    async addFn () {
      this.showDialog = true
    },
    changePage (val) {
      this.page.page = val
      this.getTagsLists()
    },
    changeSize (val) {
      this.page.pagesize = val
      this.getTagsLists()
    }
  }
}
</script>

<style scoped lang='less'>
.tags-container {

  .addBtn {
    background-color: #67c23a;
    margin-right: 0px;
  }
}
</style>

